package com.cmj.Utils;

import com.cmj.domain.entity.Menu;
import com.cmj.domain.vo.MenuTreeVo;

import java.util.List;
import java.util.stream.Collectors;

public class SystemConverter {
    private SystemConverter(){}

    public static List<MenuTreeVo> builderMenuTreeVo(List<Menu> menus) {
        List<MenuTreeVo> menuTreeVos = menus.stream()
                .map(m -> new MenuTreeVo(m.getId(),m.getMenuName(),m.getParentId(),null))
                .collect(Collectors.toList());
        List<MenuTreeVo> menuTree = menuTreeVos.stream()
                .filter(menu -> menu.getParentId().equals(0L))
                .map(menu -> menu.setChildren(getChildrenVo(menu, menuTreeVos)))
                .collect(Collectors.toList());
        return menuTree;
    }

    /**
     * 获取存入参数的 子Menu集合
     * @param menu
     * @param menus
     * @return
     */
    public static List<MenuTreeVo> getChildrenVo(MenuTreeVo menu, List<MenuTreeVo> menus) {
        List<MenuTreeVo> childrenList = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m->m.setChildren(getChildrenVo(m,menus)))
                .collect(Collectors.toList());
        return childrenList;
    }
}
